import React, {useReducer, useState} from 'react';

function App(props) {
    const [state,dispatch] = useReducer(function(prevState,action){
        const state = JSON.parse(JSON.stringify(prevState));
        if(action.type === "UP_USERNAME"){
            state.userName = action.payload;
        }else if(action.type === "UP_AGE"){
            state.age = action.payload;
        }
        return state;
    },{
        userName:"zhangsan",
        age:12
    });
    return (
        <div>
            <button onClick={()=>{
                dispatch({type:"UP_USERNAME",payload:"lisi"});
            }}>{state.userName}</button>
            <button onClick={()=>dispatch({type:"UP_AGE",payload:200})}>{state.age}</button>
        </div>
    );
}

export default App;